<?php
namespace app\api\exception;

use think\exception\Handle;
use think\Response;
use Throwable;

/**
 * 应用异常处理类
 */
class Http extends Handle
{
    public $httpStatus = 500;
    /**
     * Render an exception into an HTTP response.
     *
     * @access public
     * @param \think\Request $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {
        if ($e instanceof \think\Exception) {
            # code...
            return show($e->getCode(), $e->getMessage());
        }

        
        if ($e instanceof \think\Exception\HttpResponseException) {
            # code...
            return parent::render($request, $e);
        }

        // 添加自定义异常处理机制
        // dump($e->getStatusCode());
        if (method_exists($e, 'getStatusCode')) {
            $httpStatus = $e->getStatusCode();
        } else {
            $httpStatus = $this->httpStatus;
        }
        return show(config('status.error'), $e->getMessage(), [], $httpStatus);

        // 其他错误交给系统处理
        return parent::render($request, $e);
    }
}
